﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using edowork.com.SQL2005;
using edowork.com.Model;

namespace WebInvoicingsys.system.baseset.basedata
{
    public partial class credit_add : System.Web.UI.Page
    {
        //前台输出提示信息变量
        public string alert_msg = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ;
            }
        }

        //保存
        protected void btnSave_Click(object sender, EventArgs e)
        {
            alert_msg = "";

            edowork.com.Model.creditInfo creditInfo = new creditInfo();

            decimal Creditqty = 0;

            if (Decimal.TryParse(txt_creditqty.Text.Trim(), out Creditqty) == false)
            {
                alert_msg = String.Format(Scripts.alert_warning, "请输入信用额度或输入的信用额度格式有误");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                txt_creditqty.Focus();

                return;
            }

            creditInfo.Creditname = txt_Creditname.Text.Trim();
            creditInfo.Creditqty = Creditqty;
            creditInfo.Sysdate = DateTime.Now.ToString("yyyy-MM-dd");

            if (String.IsNullOrEmpty(creditInfo.Creditname))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请输入等级名称");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                txt_Creditname.Focus();

                return;
            }

            //取得信用等级编码
            creditInfo.Creditid = SysID.SCR_CreditID(Session["connectionString"].ToString(), ref alert_msg);


            //保存数据入数据库
            using (edowork.com.SQL2005.TranUtil tranutil = new TranUtil(Session["connectionString"].ToString()))
            {
                System.Data.SqlClient.SqlTransaction tran = null;

                try
                {
                    tran = tranutil.GetSqlTransaction();
                }
                catch (Exception er)
                {
                    alert_msg = String.Format(Scripts.alert_warning, er.Message);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                edowork.com.SQL2005.Execute exec = new Execute(Session["connectionString"].ToString());

                string sql = "";
                int result = -1;

                //保存入credit表
                sql = String.Format(creditInfo.SQL_Insert(), creditInfo.Creditid, creditInfo.Creditname, creditInfo.Creditqty, creditInfo.Sysdate);

                result = exec.ExecSQLQueryInt(sql, ref alert_msg, tran);

                if (result <= 0)
                {
                    tran.Rollback();
                    tranutil.CloseConnection();

                    alert_msg = String.Format(Scripts.alert_warning, alert_msg);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                tran.Commit();
                tranutil.CloseConnection();
            }

            txt_Creditname.Text = "";
            txt_creditqty.Text = "";

            alert_msg = String.Format(Scripts.alert_success, Errormsg.success_Save);

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
        }

        //重置
        protected void btnReset_Click(object sender, EventArgs e)
        {
            txt_Creditname.Text = "";
            txt_creditqty.Text = "";
        }

        //返回
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("/system/baseset/basedata/credit.aspx");
        }
    }
}